package com.lfy.modules.blog.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.lfy.modules.blog.domain.event.ClearHomeArticleCacheEvent;
import com.lfy.modules.blog.domain.vo.feedback.FeedbackVO;
import com.lfy.modules.blog.service.FeedbackService;
import com.lfy.modules.common.anno.Authentication;
import com.lfy.modules.common.anno.AutoUser;
import com.lfy.modules.common.custom.constants.PageConstant;
import com.lfy.modules.common.domain.BaseEnum;
import com.lfy.modules.common.domain.PageResultVO;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.domain.entity.UserEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/feedback")
public class FeedbackController {
    @Resource
    private FeedbackService feedbackService;

    /**
     * 获取反馈列表
     * @param pageSize 页大小
     * @param pageNum 当前页
     * @return
     */
    @GetMapping("/get/list")
    @Authentication("sys:feedback:get")
    public R<PageResultVO<FeedbackVO>> getFeedBackList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @AutoUser UserEntity user) {
        // 获取反馈列表
        if(pageSize > PageConstant.MAX_PAGE_SIZE){
            return R.error(BaseEnum.commonEnum.ERROR_PAGE_SIZE);
        }
        return feedbackService.getFeedbackList(pageSize, pageNum, user.getId());
    }
    @PostMapping("/delete/{id}")
    @Authentication("sys:feedback:delete")
    public R<Void> deleteFeedback(@PathVariable("id") Integer id, @AutoUser UserEntity user) {
        // 删除反馈
        R<Void> r = feedbackService.deleteFeedback(id);
        SpringUtil.publishEvent(new ClearHomeArticleCacheEvent(this, user.getId()));
        return r;
    }

    /**
     * 处理反馈
     * @param id
     * @return
     */
    @PostMapping("/modify/{id}")
    @Authentication("sys:feedback:modify")
    public R<Void> modifyFeedback(@PathVariable("id") Integer id, @AutoUser UserEntity user) {
        // 修改反馈
        R<Void> r = feedbackService.modifyFeedback(id);
        SpringUtil.publishEvent(new ClearHomeArticleCacheEvent(this, user.getId()));
        return r;
    }

}
